#include "comdebugwidget.h"
#include "ui_comdebugwidget.h"
#include <QDateTime>
#include <QTimer>
#include <QUuid>

ComDebugWidget::ComDebugWidget(QString title,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ComDebugWidget)
{
    ui->setupUi(this);
    this->setWindowTitle(title);
    QTimer *time = new QTimer(this);
    time->start(1000);
//    connect(time,&QTimer::timeout,this,[=](){
//        ui->textEdit_tx->clear();
//        ui->textEdit_tx->setText("send data uuid" + QUuid::createUuid().toString());
//        on_pushButton_send_clicked();
//    });
}

ComDebugWidget::~ComDebugWidget()
{
    delete ui;
}

void ComDebugWidget::setSerial(Serial *serial)
{
    //连接发送接收的信号槽
    connect(serial,&Serial::haveReceive,this,&ComDebugWidget::showRecive);
    connect(serial,&Serial::sendDataShow,this,&ComDebugWidget::showSendRes);
    connect(this,&ComDebugWidget::sendData,this,[=](QString data)
    {
        Serial::instance()->sendData(data);
    });
}

void ComDebugWidget::showRecive(QByteArray info)
{
    QString time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    QString text = time + " Rx: " + QString(info);
    ui->textBrowser_rx->append(text);
}

void ComDebugWidget::showSendRes(QString data, bool isOk)
{
    QString time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
    QString text = time + " Tx: " + data;
    ui->textBrowser_rx->append(text);
}


void ComDebugWidget::on_pushButton_send_clicked()
{
    QString data = ui->textEdit_tx->toPlainText();
    emit sendData(data);
}

void ComDebugWidget::on_pushButton_cleanTx_clicked()
{
    ui->textEdit_tx->clear();
}
void ComDebugWidget::on_pushButton_cleanRx_clicked()
{
    ui->textBrowser_rx->clear();
}
